package com.tangtang.core.validator;

import com.tangtang.core.StringUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by Administrator on 2014/4/10.
 */
public class MustLengthLessThanValidator implements ConstraintValidator<MustLengthLessThan, String> {

    MustLengthLessThan must;

    @Override
    public void initialize(MustLengthLessThan constraintAnnotation) {
        this.must = constraintAnnotation;
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        int length;
        if (must.chinese()) {
            length = StringUtil.lengthCN(value);
        } else {
            length = value.length();
        }
        if (length >= must.value()) {
            return false;
        }
        return true;
    }
}
